module.exports = function (server) {
    const io = require('socket.io')(server,{ cors: true })
  
    // 监视客户端与服务器的连接
    io.on("connection", (socket) => {
        console.log(socket);
        //连接加入子房间
        socket.join(socket.id);
      
        console.log("a user connected " + socket.id);
      
        socket.on("disconnect", () => {
            console.log("user disconnected: " + socket.id);
            //某个用户断开连接的时候，我们需要告诉所有还在线的用户这个信息
            socket.broadcast.emit('user disconnected', socket.id);
        });
      
        socket.on("chat message", (msg) => {
            console.log(socket.id + " say: " + msg);
            //io.emit("chat message", msg);
            socket.broadcast.emit("chat message", msg);
        });
      
        //当有新用户加入，打招呼时，需要转发消息到所有在线用户。
        socket.on('new user greet', (data) => {
            console.log(data);
            console.log(socket.id + ' greet ' + data.msg);
            socket.broadcast.emit('need connect', {
                sender: socket.id,
                msg: data.msg
            });
        });
        //在线用户回应新用户消息的转发
        socket.on('ok we connect', (data) => {
            io.to(data.receiver).emit('ok we connect', {
                sender: data.sender
            });
        });
      
        //sdp 消息的转发
        socket.on('sdp', (data) => {
            console.log('sdp');
            console.log(data.description);
            console.log('sdp:  ' + data.sender + '   to:' + data.to,'--------------------------');
            socket.to(data.to).emit('sdp', {
                description: data.description,
                sender: data.sender
            });
        });
      
        //candidates 消息的转发
        socket.on('ice candidates', (data) => {
            console.log('ice candidates:  ');
            console.log(data);
            socket.to(data.to).emit('ice candidates', {
                candidate: data.candidate,
                sender: data.sender
            });
        });
      
    });
 }
